import { setToken, removeToken } from '../utils/token'
import { setCookie, removeCookie } from '../utils/cookie'
import userApi from '../api/user'
import {Toast} from 'vant'
export default {
  // 登录
  Login ({ commit }, loginForm) {
    return new Promise(resolve => {
      userApi.memberLogin(loginForm.cellphone, loginForm.password).then(response => {
        if (response.data.success) {
          const data = response.data.re
          setToken(data.token)
          setCookie('login', true)
          commit('SET_TOKEN', data.token)
          commit('SET_LOGIN', true)
          resolve()
        } else {
          Toast(response.data.msg)
        }
      })
    })
  },
  // 前端 登出
  Logout ({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_LOGIN', false)
      removeToken()
      removeCookie('login')
      resolve()
    })
  }
}
